package com.springboot.frame.starter.intf;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;


/**
 * 前后端数据传输加密工具类
 *
 * @author pibigstar
 */
public class AesEncrypt {
    /**
     * 可配置到Constant中，并读取配置文件注入,16位,自己定义
     */
    private static final String KEY = "abcdef0123456789";

    // 偏移量
    private static final String OFFSET = "abcdef0123456789";

    // 参数分别代表 算法名称/加密模式/数据填充方式
    // JCE 中 AES 支持五中模式（CBC，CFB，ECB，OFB，PCBC）。
    // 默认ECB模式，不支持iv偏移量操作
    // private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    // 参数分别代表 算法名称/加密模式/数据填充方式
    private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";

    /**
     * 加密
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return
     * @throws Exception
     */
    public static String encrypt(String content, String encryptKey, String offset) throws Exception {
        byte[] raw = encryptKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        IvParameterSpec iv = new IvParameterSpec(offset.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] b = cipher.doFinal(content.getBytes("utf-8"));
        // 采用base64算法进行转码,避免出现中文乱码
        return new BASE64Encoder().encode(b);
    }

    /**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptStr, String decryptKey, String offset) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            IvParameterSpec iv = new IvParameterSpec(offset.getBytes());
            SecretKeySpec skeySpec = new SecretKeySpec(decryptKey.getBytes(Charset.forName("UTF-8")), "AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            // 采用base64算法进行转码,避免出现中文乱码
            byte[] encryptBytes = new BASE64Decoder().decodeBuffer(encryptStr);
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String content) throws Exception {
        return encrypt(content, KEY, OFFSET);
    }

    public static String encrypt(String content, String encryptKey) throws Exception {
        return encrypt(content, encryptKey, OFFSET);
    }

    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        return decrypt(encryptStr, decryptKey, OFFSET);
    }


    public static String decrypt(String encryptStr) throws Exception {
        return decrypt(encryptStr, KEY, OFFSET);
    }


}
